package com.xiaochen.baidu.service.thread;

import com.xiaochen.baidu.service.IMoiuLoginService;
import lombok.AllArgsConstructor;
import lombok.SneakyThrows;
import okhttp3.ResponseBody;
import retrofit2.Call;

import java.util.function.Supplier;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author xc
 * @date 2025/10/11/20:44
 * @desc
 */
@AllArgsConstructor 
public class LoginThreadTask implements Supplier<String> {
    
    private String username;
    private String password;
    private IMoiuLoginService moiuLoginService;

    @SneakyThrows
    @Override
    public String get() {
        Call<ResponseBody> oauthSession = moiuLoginService.getOauthSession(username, password);
        String result = oauthSession.execute().headers().get("Set-Cookie");
        Matcher matcher = Pattern.compile("oauth_session=[^;]+").matcher(result);
        if (matcher.find()) {
            return matcher.group(0);
        }
        return null;
    }
}
